#!/bin/sh

# This script is a backup solution for setting up/removing MCTP endpoint for NICs
action=$1
slot=$2
# NIC bus are 24~27.
bus=$((24 + slot))
# Static EID for NIC are 90~93.
eid=$((90 + slot))
# NIC version.
version=""

sleep 2

set_nic_eid() {
    MAX_RETRIES=60
    RETRY_COUNT=0
    while [ "$RETRY_COUNT" -lt "$MAX_RETRIES" ]; do
        busctl call au.com.codeconstruct.MCTP1 /au/com/codeconstruct/mctp1/interfaces/"mctpi2c${bus}" au.com.codeconstruct.MCTP.BusOwner1 AssignEndpointStatic ayy 1 0x32 "$eid"
        ret=$?
        if [ "$ret" -eq 0 ]; then
            break
        fi
        echo "Retry setting EID ${eid}"
        RETRY_COUNT=$((RETRY_COUNT + 1))
        sleep 1
    done
}

set_nic_version() {
    MAX_RETRIES=3
    RETRY_COUNT=0
    while [ "$RETRY_COUNT" -lt "$MAX_RETRIES" ]; do
        version=$(
            pldmtool fw_update GetFwParams -m $eid |
                jq --raw-output \
                    '.ComponentParameterEntries | .[].ActiveComponentVersionString'
        )

        if [ -n "$version" ]; then
            busctl set-property xyz.openbmc_project.Settings "/xyz/openbmc_project/software/chassis/nic$slot" xyz.openbmc_project.Software.Version Version "s" "$version"
            break
        else
            echo "Version is empty, retrying... ($((RETRY_COUNT + 1))/$MAX_RETRIES)"
            RETRY_COUNT=$((RETRY_COUNT + 1))
            sleep 5
        fi
    done
}

if [ "$action" = "add" ]; then
    echo "Adding MCTP endpoint for slot $slot"
    set_nic_eid
    set_nic_version
elif [ "$action" = "remove" ]; then
    echo "Removing MCTP endpoint for slot $slot"
    busctl call au.com.codeconstruct.MCTP1 /au/com/codeconstruct/mctp1/networks/1/endpoints/${eid} au.com.codeconstruct.MCTP.Endpoint1 Remove
    busctl set-property xyz.openbmc_project.Settings "/xyz/openbmc_project/software/chassis/nic$slot" xyz.openbmc_project.Software.Version Version "s" "$version"
fi
